<?php
namespace Api\Controller;
use Think\Controller;
class RegisterController extends Controller{
 //确定注册
 public function submitRegister(){
  $utypearray=array('user','doctor');
          $phone= I('phone');//手机号码
          $code = I('phonecode');//手机验证码
          $password = I('password');//用户密码
          $utype = I('utype');//1用户 2医生 
          if(!verPhone($phone)){
            $uid='';
            EchoData($uid,0,107,'手机号错误',0);
          }
          if(!in_array($utype, $utypearray)){
            $uid='';
            EchoData($uid,0,105,'utype类型错误',0);
          }
          if(empty($code)){
            $uid='';
            EchoData($uid,0,106,'请填写验证码',0);
          }
          if(empty($password)){
            $uid='';
            EchoData($uid,0,109,'密码不能为空',0);
          }
          $PhoneCode=D('PhoneCode')->verPhoneCode($phone,$code,'register',0);
          if(!$PhoneCode){
            $uid='';
            EchoData($uid,0,102,'验证码错误',0);
          }else{
           if($utype=='user'){
            $uid=D('User')->addUser($phone,md5($password));
            $uinfo=D('User')->toIdGetUserInfo($uid);
            $refont='新用户';
          }elseif($utype=='doctor'){
            $uid=D('Doctor')->addDoctor($phone,md5($password));
            $uinfo=D('Doctor')->toIdGetDoctorInfo($uid,'register');
            $refont='新医生';
          }
          if($uid){
            $reinfo='注册'.$refont.'成功';
            EchoData($uinfo,1,100,$reinfo);
          }else{
            $reinfo='注册'.$refont.'失败';
            EchoData($uinfo,0,101,$reinfo,0);
          }
        }
      }

   //小程序点击微信登录 用手机号码 和 openid注册
      public function xiaoBindPhoneRegister(){
        $utypearray=array('user','doctor');
        $utype   = I('utype');
        $openid  = I('openid');
        $phone   = I('phone');
        $code    = I('code');
        $head    = I('head');
        $headurl = 'Public/api/imgs/head/'.$openid.time().'.jpg';
        $PhoneCode=D('PhoneCode')->verPhoneCode($phone,$code,'bindphone');
        if(!$PhoneCode){
          EchoData('',0,102,'验证码错误',0);
        }
        if(!verPhone($phone)){
          EchoData('',0,107,'手机号码错误',0);
        }
        if(!in_array($utype, $utypearray)){
          EchoData('',0,105,'utype类型错误',0);
        }
        if(empty($code)){
          EchoData('',0,106,'请填写验证码',0);
        }
        $wherefind['phone']=$phone;
        $infofind= fd('User',$wherefind);
        if($infofind){ //如果找到此手机号码 此手机号码绑定此opnid 用更新 save 返回id
          $se['xiao_openid']=$openid;
          $sewhere['phone']=$phone;
          se('User',$sewhere,$se);
          $uid=$infofind['id'];
        }else{  //如果没有找到此手机号码  就是  添加新用户  add 返回id
          $username= I('username');
          $img=file_get_contents($head);
          file_put_contents($headurl, $img);
          //如果没有此用户就可以添加微信默认头像和名字end
          $ad['xiao_openid']=$openid;
          $ad['ctime'] =time();
          $ad['phone'] =$phone;
          $ad['name']  =$username;
          $ad['sex']   ='n';
          $ad['token'] =creatToken();
          $ad['head']  =$headurl;
          $uid=ad('User',$ad);
        }
        $uinfo=D('User')->toIdGetUserInfo($uid);
        if($uinfo){
          $reinfo='注册用户成功';
          EchoData($uinfo,1,100,$reinfo);
        }else{
          $reinfo='注册用户失败';
          EchoData($uinfo,0,101,$reinfo,0);
        }
        
      }
    

  }